package com.java.system.controller;

import com.java.system.entity.Menu;
import com.java.system.entity.Result;
import com.java.system.entity.User;
import com.java.system.service.ILoginService;
import com.java.system.service.IMenuService;
import com.java.system.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    ILoginService loginService;
    @Autowired
    IMenuService menuService;
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        String token = loginService.login(user);
        if(token!=null)
        {
            Result result=Result.success("生成token成功");
            result.put("token",token);
            return result;
        }
        return Result.error("生成token失败");
    }
    @GetMapping("/info")
    public Result getInfo(HttpServletRequest request) throws Exception {
        String token = request.getHeader("BASE_TOKEN");
        Claims claims = JwtUtil.parseJWT(token);
        String id = claims.getSubject();
        User user = loginService.select(id);
        List<Menu> menuList = menuService.getMenuListByUserId(user.getId());
        Result result=Result.success("获取用户信息成功");
        result.put("data",user);
        result.put("menu",menuList);
        return result;


    }
}
